package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.*;
import com.ruoyi.zhgd.mapper.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjWarningDisposalBo;
import com.ruoyi.zhgd.domain.vo.ZjWarningDisposalVo;
import com.ruoyi.zhgd.service.IZjWarningDisposalService;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 预警处置Service业务层处理
 *
 * @author zyb
 * @date 2024-03-14
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjWarningDisposalServiceImpl implements IZjWarningDisposalService {

    private final ZjWarningDisposalMapper baseMapper;

    private final ZjNWarningHntMapper hntMapper;

    private final ZjNWarningZlyjMapper zjNWarningZlyjMapper;

    private final ZjWarningSyMapper zjWarningSyMapper;

    private final ZjNWarningSwlqMapper zjNWarningSwlqMapper;

    private final ZjNWarningLqMapper zjNWarningLqMapper;

    private final ZjNWarningSwMapper zjNWarningSwMapper;

    /**
     * 查询预警处置
     */
    @Override
    public ZjWarningDisposalVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询预警处置列表
     */
    @Override
    public TableDataInfo<ZjWarningDisposalVo> queryPageList(ZjWarningDisposalBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjWarningDisposal> lqw = buildQueryWrapper(bo);
        Page<ZjWarningDisposalVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询预警处置列表
     */
    @Override
    public List<ZjWarningDisposalVo> queryList(ZjWarningDisposalBo bo) {
        LambdaQueryWrapper<ZjWarningDisposal> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjWarningDisposal> buildQueryWrapper(ZjWarningDisposalBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjWarningDisposal> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWarningId()), ZjWarningDisposal::getWarningId, bo.getWarningId());
        lqw.eq(bo.getStatus() != null, ZjWarningDisposal::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getDisposalInstructions()), ZjWarningDisposal::getDisposalInstructions, bo.getDisposalInstructions());
        lqw.eq(StringUtils.isNotBlank(bo.getDisposer()), ZjWarningDisposal::getDisposer, bo.getDisposer());
        lqw.eq(StringUtils.isNotBlank(bo.getFileUrl()), ZjWarningDisposal::getFileUrl, bo.getFileUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getWarningType()), ZjWarningDisposal::getWarningType, bo.getWarningType());
        lqw.eq(bo.getHandleTime() != null, ZjWarningDisposal::getHandleTime, bo.getHandleTime());
        lqw.eq(bo.getConfirmTime() != null, ZjWarningDisposal::getConfirmTime, bo.getConfirmTime());
        lqw.eq(bo.getDeptId() != null, ZjWarningDisposal::getDeptId, bo.getDeptId());
        return lqw;
    }

    /**
     * 新增预警处置,发送，保存草稿接口
     */
    @Override
    @Transactional
    public Boolean insertByBo(ZjWarningDisposalBo bo) {
        ZjWarningDisposal add = BeanUtil.toBean(bo, ZjWarningDisposal.class);
        validEntityBeforeSave(add);
        add.setHandleTime(new Date());
        boolean flag = baseMapper.insertOrUpdate(add);
        if (flag) {
            bo.setId(add.getId());
        }
        // 更新业务表状态；1,2,3,4,5,6   张拉，压浆，试验室，混凝土，水稳，沥青。

        String warningType = bo.getWarningType();
        if("4".equals(warningType)){
            ZjNWarningHnt hnt = new ZjNWarningHnt();
            hnt.setId(bo.getWarningId());
            hnt.setDisposalStatus(bo.getDisposer() == null ? 2 : 3);
            hntMapper.updateById(hnt);
        }
        if("1".equals(warningType) || "2".equals(warningType)){
            ZjNWarningZlyj entity = new ZjNWarningZlyj();
            entity.setId(bo.getWarningId());
            entity.setDisposalStatus(bo.getDisposer() == null ? 2 : 3);
            zjNWarningZlyjMapper.updateById(entity);
        }
        if ("3".equals(warningType)) {
            ZjWarningSy entity = new ZjWarningSy();
            entity.setId(bo.getWarningId());
            entity.setDisposalStatus(bo.getDisposer() == null ? 2 : 3);
            zjWarningSyMapper.updateById(entity);
        }
//        if ("5".equals(warningType) || "6".equals(warningType)) {
//            ZjNWarningSwlq entity = new ZjNWarningSwlq();
//            entity.setId(bo.getWarningId());
//            entity.setDisposalStatus(bo.getDisposer() == null ? 2 : 3);
//            zjNWarningSwlqMapper.updateById(entity);
//        }
        if ("5".equals(warningType)) {
            ZjNWarningSw entity = new ZjNWarningSw();
            entity.setId(bo.getWarningId());
            entity.setDisposalStatus(bo.getDisposer() == null ? 2 : 3);
            zjNWarningSwMapper.updateById(entity);
        }
        if ("6".equals(warningType)) {
            ZjNWarningLq entity = new ZjNWarningLq();
            entity.setId(bo.getWarningId());
            entity.setDisposalStatus(bo.getDisposer() == null ? 2 : 3);
            zjNWarningLqMapper.updateById(entity);
        }
        return flag;
    }

    /**
     * 修改预警处置, 撤回处置单
     */
    @Override
    @Transactional
    public Boolean updateByBo(ZjWarningDisposalBo bo) {
//        ZjWarningDisposal update = BeanUtil.toBean(bo, ZjWarningDisposal.class);
//        validEntityBeforeSave(update);
        ZjWarningDisposalVo vo = getInfoByWarning(bo.getWarningId(), Integer.parseInt(bo.getWarningType()));
        ZjWarningDisposal bean = BeanUtil.toBean(vo, ZjWarningDisposal.class);
        baseMapper.updateById(bean);
        boolean flag = baseMapper.updateById(bean) > 0;

        // 1撤回操作: 处置中->草稿状态
        if ("1".equals(bo.getOperation())) {
            // 更新业务表状态；1,2,3,4,5,6   张拉，压浆，试验室，混凝土，水稳，沥青。
            String warningType = bo.getWarningType();
            if("4".equals(warningType)){
                ZjNWarningHnt hnt = new ZjNWarningHnt();
                hnt.setId(bo.getWarningId());
                hnt.setDisposalStatus(bo.getStatus());
                hntMapper.updateById(hnt);
            }
            if("1".equals(warningType) || "2".equals(warningType)){
                ZjNWarningZlyj entity = new ZjNWarningZlyj();
                entity.setId(bo.getWarningId());
                entity.setDisposalStatus(bo.getStatus());
                zjNWarningZlyjMapper.updateById(entity);
            }
            if ("3".equals(warningType)) {
                ZjWarningSy entity = new ZjWarningSy();
                entity.setId(bo.getWarningId());
                entity.setDisposalStatus(bo.getStatus());
                zjWarningSyMapper.updateById(entity);
            }
//            if ("5".equals(warningType) || "6".equals(warningType)) {
//                ZjNWarningSwlq entity = new ZjNWarningSwlq();
//                entity.setId(bo.getWarningId());
//                entity.setDisposalStatus(bo.getStatus());
//                zjNWarningSwlqMapper.updateById(entity);
//            }
            if ("5".equals(warningType)) {
                ZjNWarningSw entity = new ZjNWarningSw();
                entity.setId(bo.getWarningId());
                entity.setDisposalStatus(bo.getStatus());
                zjNWarningSwMapper.updateById(entity);
            }
            if ("6".equals(warningType)) {
                ZjNWarningLq entity = new ZjNWarningLq();
                entity.setId(bo.getWarningId());
                entity.setDisposalStatus(bo.getStatus());
                zjNWarningLqMapper.updateById(entity);
            }
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjWarningDisposal entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除预警处置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public R<List<SysUser>> selectUserList(String substring,String ancestor) {
        List<SysUser> list = baseMapper.selectUserList(substring,ancestor);
        return R.ok(list);
    }

    @Override
    public ZjWarningDisposalVo getInfoByWarning(String id, int type) {
        LambdaQueryWrapper<ZjWarningDisposal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZjWarningDisposal::getWarningId,id);
        wrapper.eq(ZjWarningDisposal::getWarningType,type);
        return baseMapper.selectVoOne(wrapper);
    }

    /**
     * 驳回：5，同意：4；
     * @param bo
     * @return
     */
    @Transactional
    @Override
    public int handleApproval(ZjWarningDisposalBo bo) {
        ZjWarningDisposal disposal = new ZjWarningDisposal();
        disposal.setId(bo.getId());
        disposal.setStatus(bo.getStatus());
        disposal.setConfirmTime(new Date());
        disposal.setApprovalOpinions(bo.getApprovalOpinions());
        baseMapper.updateById(disposal);
        String warningType = bo.getWarningType();
        if("4".equals(warningType)){
            ZjNWarningHnt hnt = new ZjNWarningHnt();
            hnt.setId(bo.getWarningId());
            hnt.setDisposalStatus(bo.getStatus());
            hntMapper.updateById(hnt);
            return 1;
        }
        if("1".equals(warningType) || "2".equals(warningType)){
            ZjNWarningZlyj entity = new ZjNWarningZlyj();
            entity.setId(bo.getWarningId());
            entity.setDisposalStatus(bo.getStatus());
            zjNWarningZlyjMapper.updateById(entity);
            return 1;
        }
        if ("3".equals(warningType)) {
            ZjWarningSy entity = new ZjWarningSy();
            entity.setId(bo.getWarningId());
            entity.setDisposalStatus(bo.getStatus());
            zjWarningSyMapper.updateById(entity);
            return 1;
        }
//        if ("5".equals(warningType) || "6".equals(warningType)) {
//            ZjNWarningSwlq entity = new ZjNWarningSwlq();
//            entity.setId(bo.getWarningId());
//            entity.setDisposalStatus(bo.getStatus());
//            zjNWarningSwlqMapper.updateById(entity);
//            return 1;
//        }
        if ("5".equals(warningType)) {
            ZjNWarningSw entity = new ZjNWarningSw();
            entity.setId(bo.getWarningId());
            entity.setDisposalStatus(bo.getStatus());
            zjNWarningSwMapper.updateById(entity);
            return 1;
        }
        if ("6".equals(warningType)) {
            ZjNWarningLq entity = new ZjNWarningLq();
            entity.setId(bo.getWarningId());
            entity.setDisposalStatus(bo.getStatus());
            zjNWarningLqMapper.updateById(entity);
            return 1;
        }

        return 1;
    }
}
